//
//  AppDelegate.swift
//  eBooks
//
//  Created by 赵俊明 on 2019/1/16.
//  Copyright © 2019 赵俊明. All rights reserved.
//

import UIKit
import GoogleMobileAds
import Alamofire
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        downloadEpubAndCopyDB()
        startListen()
        
        GADMobileAds.sharedInstance().start(completionHandler: nil)
        
        // GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = ["99bf284c9dbbf7ed4c9c5dbfd935cd88"]
        // Tools.setVip(true)
        // print("docu:\(Tools.documentPath()!)")
        
        let umconfig = UMAnalyticsConfig.sharedInstance()
        umconfig?.appKey = Tools.umengKey()
        umconfig?.channelId = "AppStore"
        MobClick.start(withConfigure: umconfig)
        let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
        MobClick.setAppVersion(version)
        
        return true
    }
    
    func downloadEpubAndCopyDB() {
        if !EBookDefault.readBoolByKey("download") {
            NetHelper.share.downloadWith(urlStr: Tools.epubUrl(), success: { (desc, status) in
                if status {
                    // 标记写入成功
                    EBookDefault.setBool(true, "download")
                    if let model = SQLHelper.share.fetchSigleModel("little prince.epub",true) {
                        // 本地存在该资源
                        model.isdel = false
                        SQLHelper.share.insertLocalDB(model)
                    }else {
                        // 本地不存在该资源，读取资源名字，size
                        let model = FileHelper.singleModel("little prince.epub")
                        model.isdel = false
                        SQLHelper.share.insertLocalDB(model)
                    }
                }else {
                    print("😭😭😭😭😭下载失败")
                }
            }) { (_)  in}
        }
    }
    
    func confignav() {
         UINavigationBar.appearance().tintColor = UIColor.orange
    }
    
    func startListen() {
        let net = NetworkReachabilityManager()
        net?.startListening()
        net?.listener = {status in
            if let status = net?.isReachable, status == false {
                let item = SheetItem(title: "⚠️当前网络不可用😭，请检查网络😔")
                item.className = "BookTopAlert"
                item.sheetType = .top
                item.canPierce = true
                item.bkgColor = UIColor.black.component(0.0)
                SheetManager(superView: nil, item: item).update()
            }
            
//            if net?.isReachable ?? false{
//                switch status{
//                case .notReachable,.unknown:
//                    print("the noework is not reachable")
//                    toast = "当前网络状态不稳定！😔"
//                case .reachable(.ethernetOrWiFi),.reachable(.wwan):
//                    print("当前正在使用Wi-Fi，请放心使用！😄")
//                    toast = ""
//                }
//            } else {
//                print("当前网络不可用😭")
//                toast = "当前网络不可用😭"
//                let item = SheetItem(title: toast)
//                item.className = "BookTopAlert"
//                item.sheetType = .top
//                item.canPierce = true
//                item.bkgColor = UIColor.black.component(0.0)
//                SheetManager(superView: nil, item: item).update()
//            }
        }
    }
}

